package com.gzsxy.rabbitmq.producer.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;


/**
 * @Description: 延时队列管理
 * @Author: xiaolong
 * @date: 2023/2/13 16:30
 * @Version: 1.0
 */
@Slf4j
@RequestMapping("ttl")
@RestController
@Api(tags = "延时队列管理模块")
public class TtlSendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    /**
     * 延时队列测试
     * @param message 消息
     */
    @GetMapping("sendMsg/{message}")
    @ApiOperation(value = "延时队列测试")
    public void sendMsg(@PathVariable String message) {
        log.info("当前时间：{},发送一条信息给两个 TTL 队列:{}", new Date(), message);
        //定义CorrelationData对象以及消息属性。不然comfirm方法无论失败还是成功，CorrelationData参数永远是null
//        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
//        //传递业务数据
//        correlationData.setReturnedMessage(new Message(JSONObject.toJSON("消息来自 ttl 为 10S 的队列: ").toString().getBytes(StandardCharsets.UTF_8),new MessageProperties()));

        rabbitTemplate.convertAndSend("X", "XA", "消息来自 ttl 为 10S 的队列: " + message);
        rabbitTemplate.convertAndSend("X", "XB", "消息来自 ttl 为 40S 的队列: " + message);
    }


    /**
     * 延时队列优化
     * @param message 消息
     * @param ttlTime 延时的毫秒
     */
    @GetMapping("sendExpirationMsg/{message}/{ttlTime}")
    @ApiOperation(value = "延时队列优化，细化消息过期时间")
    public void sendMsg(@PathVariable String message, @PathVariable String ttlTime) {
        rabbitTemplate.convertAndSend("X", "XC", message, correlationData -> {
            correlationData.getMessageProperties().setExpiration(ttlTime);
            return correlationData;
        });
        log.info("当前时间：{},发送一条时长{}毫秒 TTL 信息给队列 C:{}", new Date(), ttlTime, message);
    }

    /**
     * 使用rabbitmq_delayed_message_exchange 插件实现延时队列优化
     * @param message 消息
     * @param delayTime 延时的毫秒
     */
    @GetMapping("sendDelayMsg/{message}/{delayTime}")
    @ApiOperation(value = "使用rabbitmq_delayed_message_exchange 插件实现延时队列")
    public void sendMsg(@PathVariable String message, @PathVariable Integer delayTime) {
        rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME, DELAYED_ROUTING_KEY, message,
                correlationData -> {
                    correlationData.getMessageProperties().setDelay(delayTime);
                    return correlationData;
                });
        log.info(" 当 前 时 间 ： {}, 发 送 一 条 延 迟 {} 毫秒的信息给队列 delayed.queue:{}", new Date(), delayTime, message);
    }
}
